﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SunshineAirlines
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        //public delegate string value(string value);
        ////public event value valuechange;
        //string v = "";
        //public UserControl1(string v)
        //{
        //    this.v = v;
        ////}
        //public delegate void Change();
        //public event Change selectChange;
        private void label1_Click(object sender, EventArgs e)
        {

        }
        //Form 变量 frm,而在窗体函数中， frm 被赋值 frm1;这个frm 又是从窗体form1来的。就好比传递一个参数
        Form f1;
        public UserControl1(FoodServices f1)
        {
            this.f1 = f1;
            InitializeComponent();
        }

        private int a = 0;
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            //注意转换
            string sql = "select Price from FlightFood where Name='" + label1.Text.ToString() + "'";
            double unitPrice = float.Parse(DBHelper.ExecuteScalar(sql).ToString());
            double p = float.Parse(numericUpDown1.Value.ToString()) * unitPrice;
            label3.Text = p.ToString();
            //DBHelper.payment = DBHelper.payment + (int)p;
            //f1.Controls["label6"].Text = ""+ DBHelper.payment.ToString()+"";
            //DBHelper.amount = DBHelper.amount + (int)numericUpDown1.Value;
            //f1.Controls["label5"].Text=""+DBHelper.amount+"";

            if (a > numericUpDown1.Value)
            {
                DBHelper.amount = DBHelper.amount - 1;
                DBHelper.payment = DBHelper.payment - unitPrice;
                a--;

            }
            if (a < numericUpDown1.Value)
            {
                DBHelper.amount = DBHelper.amount + 1;
                DBHelper.payment = DBHelper.payment + unitPrice;
                a++;
            }
            f1.Controls["label6"].Text = "" + DBHelper.payment.ToString() + "";
            f1.Controls["label5"].Text = "" + DBHelper.amount + "";

            DBHelper.foodAmount[int.Parse(this.Name)] = p;
        }
        private void UserControl1_Load(object sender, EventArgs e)
        {
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            {
                DBHelper.items += 1;
            }
            else
            {
                DBHelper.items -= 1;
            }
            f1.Controls["label4"].Text = "" + DBHelper.items.ToString() + "";

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int i = int.Parse(this.Name);
            if (DBHelper.foodAmount.ContainsKey(i))
            {
                if (checkBox1.Checked)
                {
                    DBHelper.foodAmount[i] = int.Parse(label3.Text);
                }
                else
                {
                    DBHelper.foodAmount[i] = 0;
                }
            }
            else
            {
                if (checkBox1.Checked)
                {
                    DBHelper.foodAmount.Add(i, 0);
                }
            }
            //if (checkBox1.Checked)
            //{
            //    DBHelper.fooid[i] = i;
            //    // 1 空字典 checked-> add kvp; else pass
            //    // key exist checked-> upd val; else clear val 

            //}
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
